﻿using System;
using System.Collections.Generic;
using System.Data;
using WFC.BL.Interfaces.DomainModels;
using WFC.BL.Interfaces.Enumerations;
using WFC.BL.Interfaces.Repositories;
using WFC.BL.Interfaces.Utilities;
using WFC.BL.Utilities;
using WFC.BL.DomainModels;
using WFC.DAL.Interfaces.PersistentStorage;
using WFC.DAL.PersistentStorage;

namespace WFC.BL.Repositories
{
    public class RefDataRepository : IRefDataRepository
    {

        private IRefDataService Service { get; set; }
        private IUtilityService Utility { get; set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        public RefDataRepository()
        {
            Service = new RefDataService();
            Utility = new UtilityService();
        }

        #region IRefDataRepository Members

        public IList<IValueType> ReadRefDataByCategory(RefDataCategory category)
        {
            var categoryId = Convert.ToInt32(category);
            return ReadRefDataByCategory(categoryId);
        }

        public IList<IValueType> ReadRefDataByCategory(int categoryId)
        {
            var dt = Service.ReadRefDataByCategoryId(categoryId).Tables[0];
            var refData = new List<IValueType>();

            if (dt.Rows == null || dt.Rows.Count == 0) { return refData; }
                
            foreach (DataRow row in dt.Rows)
            {
                var type = Utility.MapDataRowToEntity<ValueTypeBase>(row);
                refData.Add(type);
            }

            return refData;
        }

        #endregion

    }
}
